#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*f;
}
int wtp,wstk[40];
void write(int x,char c){
    if(x<0) x=-x,putchar('-');
    do{wstk[++wtp]=x%10,x/=10;}while(x);
    while(wtp){putchar(wstk[wtp--]^48);}
    if(c) putchar(c);
}
int n,m,K,bd,flag=1,_a[N],a[N],c[N],cc[N],rl[N<<3],ans1[N<<3],ans2[N<<3],ans[N];char str[18][N];
struct node{int l,r,R,G,dir;}tr[N<<3];
#define ls (p<<1)
#define rs (p<<1|1)
#define mid (l+r>>1)
void build(int p,int l,int r,int R,int G){
    tr[p]={l,r,R,G,-1};if(l==r) return;
    build(ls,l,mid,R-1,2*G-1),build(rs,mid+1,r,R-1,2*G);
}
void rebuild(int p,int l,int r){
    ans1[p]=-1,rl[p]=0;
    if(tr[p].R==0){if(tr[p].G<=n) tr[p].dir=a[tr[p].G];else tr[p].dir=0;return;}
    tr[p].dir=str[tr[p].R][tr[p].G]-'0',rebuild(ls,l,mid),rebuild(rs,mid+1,r);
}
void solve(int p,int l,int r){
    if(l==r){return ans1[p]=l,ans2[p]=tr[p].dir,void();}
    solve(ls,l,mid),solve(rs,mid+1,r);
    if(tr[p].dir==0){
        if(ans2[ls]>=tr[p].R) ans1[p]=ans1[ls],ans2[p]=ans2[ls];
        else ans1[p]=ans1[rs],ans2[p]=ans2[rs];
    }
    else{
        if(ans2[rs]>=tr[p].R) ans1[p]=ans1[rs],ans2[p]=ans2[rs];
        else ans1[p]=ans1[ls],ans2[p]=ans2[ls];
    }
}
#undef ls
#undef rs
#undef mid
int calc(int x){int res=0;while((1<<res)<x) res++;return res;}
void make_real(int p){rl[p]=1,bd=max(bd,tr[p].r+1);}
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    n=read(),m=read();
    for(int i=1;i<=n;i++) _a[i]=read();
    for(int i=1;i<=m;i++){
        c[i]=read(),cc[c[i]]=i;
        if(c[i]!=(1<<calc(c[i]))) flag=0;
    }
    while((1<<K)<n) K++;
    for(int i=1;i<=K;i++) scanf("%s",str[i]+1);
    build(1,1,(1<<K),K,1);
    for(int T=read(),X[4],res=0;T;T--,res=0){
        for(int i=0;i<4;i++) X[i]=read();
        for(int i=1;i<=n;i++) a[i]=(_a[i]^X[i%4]);
        rebuild(1,1,(1<<K)),bd=1;
        if(flag){
            solve(1,1,(1<<K));
            for(int i=1;i<=m;i++){
                int tmp=(1<<K)-1+c[i];
                while(tmp!=(1<<calc(tmp))) tmp>>=1;
                res^=(i*ans1[tmp]);
            }
            write(res,'\n');
            continue;
        }
        for(int i=1;i<=n;i++){
            int st=(1<<K)-1+i,p=st,tm=K;
            ans1[p]=p-((1<<K)-1),ans2[p]=a[p-((1<<K)-1)],make_real(st);
            for(int j=1;j<=tm;j++,p>>=1){
                //cout<<p<<" "<<ans1[p]<<" "<<ans2[p]<<endl;
                if(tr[p>>1].dir==p%2){
                    if(ans2[p]>=tr[p>>1].R) ans1[p>>1]=ans1[p],ans2[p>>1]=ans2[p],make_real(p>>1);
                    else break;
                }
                else if(!rl[p>>1]){
                    ans1[p>>1]=ans1[p],ans2[p>>1]=ans2[p];
                    if(p%2) make_real(p>>1);
                }
                else break;
            }
            for(int j=1;j<=calc(i);j++) st/=2;
            ans[i]=ans1[st];
            if(bd<=(1<<calc(i))) ans[i]+=(bd+(1<<calc(i)))*((1<<calc(i))-bd+1)/2;
            //cout<<i<<" : "<<ans1[st]<<" "<<bd<<" to "<<(1<<calc(i))<<endl;
        }
        for(int i=1;i<=m;i++) res^=(i*ans[c[i]]);
        write(res,'\n');
    }
    return 0;
}
